package com.darwin.darwinUI.containers
{
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.layout.LocateRect;
	import com.darwin.darwinUI.uiconst.LocateType;
	import com.darwin.darwinUI.uiconst.UIAlign;
	
	import flash.display.DisplayObject;
	import flash.utils.Dictionary;
	
	import spark.components.Group;
	

	/**
	 * 跟随布局
	 * @author vanCopper
	 * 
	 */	
	
	
	public class Layer extends ContainerBase implements ILayer
	{
		public function Layer()
		{
			super();
			_locateRectDic = new Dictionary();
			_locateRectDic["this"] = 0;
		}
		
		/**
		 * 当前的定位矩形列表
		 */
		private var _locateRectDic:Dictionary;
		
		public function addLocateRect(locateRect:LocateRect):void
		{
			locateRect.owenr = this;
			_locateRectDic[locateRect] = 0;
		}
		public function removeLocateRect(locateRect:LocateRect):void
		{
			locateRect.owenr = null;
			delete _locateRectDic[locateRect];
		}
		
		
		
		private var _layout:String = null;
		/**
		 * 当前容器布局
		 * UIAlign.LAYOUT_VERTICAL
		 * UIAlign.LAYOUT_HORIZONTAL
		 */
		public function get layout():String
		{
			return _layout;
		}
		
		public function set layout(value:String):void
		{
			_layout = value;
			_layoutChanged = true;
			if(this.stage) this.stage.invalidate();
		}
		
		private var _align:String;
		
		/**
		 * 如果layout为null则align属性无效
		 */
		public function get align():String
		{
			return _align;
		}
		
		public function set align(value:String):void
		{
			_align = value;
			_layoutChanged = true;
			if(this.stage) this.stage.invalidate();
		}
		
		
		
		private var _gap:Number = 0;
		
		/**
		 * 如果layout为null则gap属性无效
		 */
		public function get gap():Number
		{
			return _gap;
		}
		
		public function set gap(value:Number):void
		{
			_gap = value;
			_layoutChanged = true;
			if(this.stage) this.stage.invalidate();
		}
		
		/**
		 * 布局改变标记
		 */
		private var _layoutChanged:Boolean = true;
		
		
		public function locateInform(locateRect:LocateRect):void
		{
			_layoutChanged = true;
			if(this.stage) this.stage.invalidate();
		}
		
		
		private function executeLayout():void
		{
			if(_layoutChanged)
			{
				//先将坐标原始值置0
				for(var item:Object in _locateRectDic)
				{
					_locateRectDic[item] = 0;
				}
				
				var $length:int = this.numItems;
				for(var i:int = 0;i<$length;i++)
				{
					var $ui:UIComponent = getItemAt(i);
					if($ui.locateRect == null)
					{
						if(this.layout == null)
						{//自由模式
							executeLayoutDefaultH($ui,this.width - rightDistance,0);
							executeLayoutDefaultV($ui,this.height - bottomDistance,0);
						}else if(this.layout == UIAlign.LAYOUT_HORIZONTAL){
							executeLayoutHorizontal($ui,"this",this.height - this.bottomDistance,0,0,this.align,this.gap);
						}else if(this.layout == UIAlign.LAYOUT_VERTICAL){
							executeLayoutVertical($ui,"this",this.width - this.rightDistance,0,0,this.align,this.gap);
						}
					}else{
						var $locateRect:LocateRect = $ui.locateRect;
						if($locateRect.layout == null)
						{
							executeLayoutDefaultH($ui,$locateRect.width,$locateRect.x);
							executeLayoutDefaultV($ui,$locateRect.height,$locateRect.y);
						}else if($ui.locateRect.layout == UIAlign.LAYOUT_HORIZONTAL){
							executeLayoutHorizontal($ui,$locateRect,$locateRect.height,$locateRect.x,$locateRect.y,$locateRect.align,$locateRect.gap);
						}else if($ui.locateRect.layout == UIAlign.LAYOUT_VERTICAL){
							executeLayoutVertical($ui,$locateRect,$locateRect.height,$locateRect.x,$locateRect.y,$locateRect.align,$locateRect.gap);
						}
					}//if
				}//for 
			}
			_layoutChanged = false;
		}
		
		private function executeLayoutHorizontal(ui:UIComponent,item:Object,lHeight:Number,sX:Number,sY:Number,cAlign:String,itemGap:Number):void
		{
			ui.x = _locateRectDic[item] + sX;
			var $gap:Number = itemGap;
			_locateRectDic[item] = _locateRectDic[item]+ui.width + $gap;
			
			if(cAlign == UIAlign.TOP)
			{
				ui.y = sY + ui.offsetY;
			}else if(cAlign == UIAlign.CENTER){
				ui.y = (lHeight - ui.height)/2 + sY;
			}else if(cAlign == UIAlign.BOTTOM){
				ui.y = (lHeight - ui.height) + sY;
			}else{
				ui.y = sY;
			}
		}
		
		private function executeLayoutVertical(ui:UIComponent,item:Object,lWidth:Number,sX:Number,sY:Number,cAlign:String,itemGap:Number):void
		{
			ui.y = _locateRectDic[item] + sY;
			var $gap:Number = itemGap;
			_locateRectDic[item] = _locateRectDic[item]+ui.height + $gap;
			
			if(cAlign == UIAlign.LEFT)
			{
				ui.x = sX;
			}else if(cAlign == UIAlign.CENTER){
				ui.x = (lWidth - ui.width)/2 + sX;
			}else if(cAlign == UIAlign.RIGHT){
				ui.x = (lWidth - ui.width) + sX;
			}else{
				ui.x = sX;
			}
		}
		
		
		private function executeLayoutDefaultH(ui:UIComponent,lWidth:Number,sX:Number):void
		{
			if(ui.horizontalLocate == LocateType.INDEPENDENT)
			{//自由的
				ui.x = sX + ui.x;
			}else if(ui.horizontalLocate == LocateType.LTRB){
				if(!isNaN(ui.left) && !isNaN(ui.right))
				{//两边设置的
					ui.x = ui.left+sX;
					var $w:Number = lWidth - ui.left - ui.right;
					if(ui.width != $w) ui.width = $w;
				}else if(!isNaN(ui.left) && isNaN(ui.right))
				{//左边
					ui.x = ui.left+sX;
				}else if(isNaN(ui.left) && !isNaN(ui.right))
				{//右边
					ui.x = sX + lWidth - ui.width - ui.right;
				}else{//默认
					ui.x = sX;
				}
			}else if(ui.horizontalLocate == LocateType.CENTER){
				ui.x = sX + (lWidth - ui.width)/2 + ui.offsetX;
			}else if(ui.horizontalLocate == LocateType.FOLLOW_AFTER || ui.horizontalLocate == LocateType.FOLLOW_BEFORE){
				var $followItem:UIComponent;
				if(ui.horizontalFollowObject is UIComponent)
				{
					$followItem = ui.horizontalFollowObject as UIComponent;
				}else{
					$followItem = getItemByName(ui.horizontalFollowObject.toString());
				}
				if(ui.horizontalLocate == LocateType.FOLLOW_AFTER)
				{
					if($followItem != null) ui.x = $followItem.x + $followItem.width + ui.offsetX;
				}else if(ui.horizontalLocate == LocateType.FOLLOW_BEFORE){
					if($followItem != null) ui.x = $followItem.x - ui.width + ui.offsetX;
				}
			}
		}
		
		
		private function executeLayoutDefaultV(ui:UIComponent,lHeight:Number,sY:Number):void
		{
			if(ui.verticalLocate == LocateType.INDEPENDENT)
			{//自由的
				ui.y = sY + ui.y;
			}else if(ui.verticalLocate == LocateType.LTRB){
				if(!isNaN(ui.top) && !isNaN(ui.bottom))
				{//上下边设置的
					ui.y = ui.top+sY;
					var $h:Number = lHeight - ui.top - ui.bottom;
					if(ui.height != $h) ui.height = $h;
				}else if(!isNaN(ui.top) && isNaN(ui.bottom))
				{//上边
					ui.y = ui.top+sY;
				}else if(isNaN(ui.top) && !isNaN(ui.bottom))
				{//下边
					ui.y = sY + lHeight - ui.height - ui.bottom;
				}else{//默认
					ui.y = sY;
				}
			}else if(ui.verticalLocate == LocateType.CENTER){
				ui.y = sY + (lHeight - ui.height)/2 + ui.offsetY;
			}else if(ui.verticalLocate == LocateType.FOLLOW_AFTER || ui.verticalLocate == LocateType.FOLLOW_BEFORE){
				var $followItem:UIComponent;
				if(ui.verticalFollowObject is UIComponent)
				{
					$followItem = ui.verticalFollowObject as UIComponent;
				}else{
					$followItem = getItemByName(ui.verticalFollowObject.toString());
				}
				if(ui.verticalLocate == LocateType.FOLLOW_AFTER)
				{
					if($followItem != null) ui.y = $followItem.y + $followItem.height + ui.offsetY;
				}else if(ui.verticalLocate == LocateType.FOLLOW_BEFORE){
					if($followItem != null) ui.y = $followItem.y - ui.height + ui.offsetY;
				}
			}
		}
		
		
		/**
		 * 通过一个name取得一个UIComponent
		 */
		public function getItemByName(itemName:String):UIComponent
		{
			var $count:int = this.numItems;
			for(var i:int = 0;i<$count;i++)
			{
				if(getItemAt(i).name == itemName)
				{
					return getItemAt(i);
				}
			}
			return null;
		}
		
		override public function addItem(item:UIComponent):void
		{
			super.addItem(item);
			item.owner = this;
			
			_layoutChanged = true;
			if(this.stage) this.stage.invalidate();
		}
		
		override public function addItemAt(item:UIComponent, index:int):void
		{
			super.addItemAt(item, index);
			item.owner = this;
			_layoutChanged = true;
			if(this.stage) this.stage.invalidate();
		}
		
		override public function removeAllItem():void
		{
			for(var i:int = 0;i<this.numChildren;i++)
			{
				this.getItemAt(i).owner = null;
			}
			super.removeAllItem();
			_layoutChanged = true;
			if(this.stage) this.stage.invalidate();
		}
		
		override public function removeItem(item:UIComponent):void
		{
			item.owner = null;
			_layoutChanged = true;
			if(this.stage) this.stage.invalidate();
			super.removeItem(item);
		}
		
		override public function removeItemAt(index:int):UIComponent
		{
			var $item:UIComponent = super.removeItemAt(index);
			$item.owner = null;
			_layoutChanged = true;
			if(this.stage) this.stage.invalidate();
			return $item;
		}
		
		override public function set height(value:Number):void
		{
			// TODO Auto Generated method stub
			super.height = value;
			_layoutChanged = true;
			if(this.stage) this.stage.invalidate();
		}
		
		override public function set width(value:Number):void
		{
			// TODO Auto Generated method stub
			super.width = value;
			_layoutChanged = true;
			if(this.stage) this.stage.invalidate();
		}
		
		
		
		
		
		
		
		override protected function updateDisplayList():void
		{
			super.updateDisplayList();
			executeLayout();
		}
		
		public function updateInform(uiComponent:UIComponent = null):void
		{
			_layoutChanged = true;
			if(this.stage) this.stage.invalidate();
		}
		
		
	}
} 
